//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package external_events_api_serv

import (
	"context"
	"fmt"
	"time"

	"atomgit.com/openlinksaas/api-server/api_common"
	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/event_dao"
	"atomgit.com/openlinksaas/api-server/dao/project_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_external_event"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
	"atomgit.com/openlinksaas/proto-gen-go.git/external_events_api"
	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/mongo"
	"google.golang.org/protobuf/types/known/anypb"
)

func (apiImpl *ExternalEventsApiImpl) addCreateEvent(
	sessionItem *dao.SessionDbItem,
	req *external_events_api.CreateRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	eventSourceId string, nowTime int64) {
	eventData, err := anypb.New(&events_external_event.CreateEvent{
		EventSourceId: eventSourceId,
		EventSource:   int32(req.EventSource),
		Title:         req.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_PROJECT,
			EventTime:       nowTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_PROJECT,
			RefId:           req.ProjectId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ExternalEventsApiImpl) addUpdateEvent(
	sessionItem *dao.SessionDbItem,
	req *external_events_api.UpdateRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	eventSourceItem *event_dao.EventSourceInfoDbItem) {
	eventData, err := anypb.New(&events_external_event.UpdateEvent{
		EventSourceId: req.EventSourceId,
		EventSource:   eventSourceItem.EventSource,
		OldTitle:      eventSourceItem.Title,
		NewTitle:      req.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_PROJECT,
			EventTime:       eventTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_PROJECT,
			RefId:           req.ProjectId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ExternalEventsApiImpl) addGetSecretEvent(
	sessionItem *dao.SessionDbItem,
	req *external_events_api.GetSecretRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	eventSourceItem *event_dao.EventSourceInfoDbItem) {
	eventData, err := anypb.New(&events_external_event.GetSecretEvent{
		EventSourceId: req.EventSourceId,
		EventSource:   eventSourceItem.EventSource,
		Title:         eventSourceItem.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_PROJECT,
			EventTime:       eventTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_PROJECT,
			RefId:           req.ProjectId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ExternalEventsApiImpl) addRemoveEvent(
	sessionItem *dao.SessionDbItem,
	req *external_events_api.RemoveRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	eventSourceItem *event_dao.EventSourceInfoDbItem) {
	eventData, err := anypb.New(&events_external_event.RemoveEvent{
		EventSourceId: req.EventSourceId,
		EventSource:   eventSourceItem.EventSource,
		Title:         eventSourceItem.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_PROJECT,
			EventTime:       eventTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_PROJECT,
			RefId:           req.ProjectId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ExternalEventsApiImpl) addSetSourceUserPolicyEvent(
	sessionItem *dao.SessionDbItem,
	req *external_events_api.SetSourceUserPolicyRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	eventSourceItem *event_dao.EventSourceInfoDbItem,
	sourceUserItem *event_dao.SourceUserInfoDbItem,
	mapUserDisplayName string) {
	eventData, err := anypb.New(&events_external_event.SetSourceUserPolicyEvent{
		EventSourceId:      req.EventSourceId,
		EventSource:        eventSourceItem.EventSource,
		Title:              eventSourceItem.Title,
		SourceUserName:     sourceUserItem.SourceUserName,
		SourceDisplayName:  sourceUserItem.SourceDisplayName,
		UserPolicy:         int32(req.UserPolicy),
		MapUserId:          req.MapUserId,
		MapUserDisplayName: mapUserDisplayName,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_PROJECT,
			EventTime:       eventTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_PROJECT,
			RefId:           req.ProjectId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}
